var _extends = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var o, a = arguments[e];
        for (o in a) Object.prototype.hasOwnProperty.call(a, o) && (t[o] = a[o]);
    }
    return t;
}, _tools = require("../../utils/tools");

function _toConsumableArray(t) {
    if (Array.isArray(t)) {
        for (var e = 0, o = Array(t.length); e < t.length; e++) o[e] = t[e];
        return o;
    }
    return Array.from(t);
}

function _defineProperty(t, e, o) {
    return e in t ? Object.defineProperty(t, e, {
        value: o,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = o, t;
}

var g, toid, pages, iosTop, fromid, is_cancel, top, right, bottom, left, interval, chat, app = getApp(), page = app.page, request = app.util.request, route = app.route, RManager = wx.getRecorderManager(), audioContext = wx.createInnerAudioContext(), time = 1, ONE_MINUTE = 60;

function onStop() {
    g.setData(_defineProperty({}, chat, ""));
}

function getChatList() {
    var a = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : 1;
    request({
        url: "Chat",
        data: {
            type: "init",
            page: a,
            toid: toid,
            fromid: fromid,
            number: app.config.chat_number
        },
        success: function(t) {
            for (var e, o = 0; o < t.length; o++) t[o].time = (0, _tools.getDateString)(t[o].time);
            g.data.chat.length && (e = g.data.chat[0].id), g.setData({
                chat: [].concat(_toConsumableArray(t), _toConsumableArray(g.data.chat))
            }, function() {
                (0, _tools.scrollBottom)(1 == a ? void 0 : "#item_" + e);
            });
        }
    }), request({
        url: "Chat",
        data: {
            type: "change_state",
            toid: toid,
            fromid: fromid
        },
        showLoading: !1,
        success: function(t) {
            if (app.chatList) {
                for (var e, o = 0, a = 0; a < app.chatList.length; a++) if (app.chatList[a].toid == toid) {
                    o = a;
                    break;
                }
                app.chatList[o].countNoread = 0, app.chatListThat && (e = "chatList[" + o + "].countNoread", 
                app.chatListThat.setData(_defineProperty({}, e, 0)));
            }
        }
    });
}

function send() {
    var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : g.data.content, a = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 1, e = arguments[2];
    (0, _tools.sendMessage)({
        content: t,
        toid: toid,
        message_type: a,
        duration: e,
        success: function(t) {
            var e, o = "chat[" + (g.data.chat ? g.data.chat.length : 0) + "]";
            t.time = (0, _tools.getDateString)(t.time), g.setData((_defineProperty(e = {}, o, t), 
            _defineProperty(e, "content", ""), _defineProperty(e, "is_send", !0), e), function() {
                (0, _tools.scrollBottom)("#chat"), 2 == a && wx.hideLoading();
            });
        }
    });
}

page({
    data: {
        user_id: parseInt(app.userInfo.id || wx.getStorageSync("userInfo").id),
        is_send: !0,
        chat: [],
        is_voice: !1,
        is_touch: !1,
        is_dis: "",
        law_id: ""
    },
    tobuy: function() {
        route({
            url: "lawyer_info",
            params: {
                id: g.data.law_id
            }
        });
    },
    onLoad: function(t) {
        g = this, chat = "", wx.getStorageSync("page") ? g.setData({
            is_dis: 2
        }) : request({
            url: "CheckImOrder",
            data: {
                user_id: app.userInfo.id || wx.getStorageInfoSync().userInfo.id,
                toid: t.toid
            },
            success: function(t) {
                g.setData({
                    is_dis: t && 1 == t.is_chat ? 2 : 1,
                    law_id: t && t.law_id ? t.law_id : ""
                });
            }
        }), RManager.onInterruptionBegin(function(t) {
            (0, _tools.toast)("语音中断");
        }), RManager.onStop(function(e) {
            if (clearInterval(interval), time = 1, g.setData({
                time: "00",
                is_touch: !1
            }), !is_cancel) {
                if (e.duration < 1e3) return (0, _tools.toast)("语音太短");
                var t = getApp().siteInfo.siteroot + "?i=" + getApp().siteInfo.uniacid + "&c=entry&a=wxapp&do=UploadAudio&m=" + getApp().config.module;
                wx.uploadFile({
                    filePath: e.tempFilePath,
                    url: t,
                    name: "upfile",
                    success: function(t) {
                        return send(t.data, 3, parseInt(e.duration / 1e3));
                    }
                });
            }
        }), RManager.onStart(function(t) {
            interval = setInterval(function() {
                time <= ONE_MINUTE && (g.setData({
                    time: time < 10 ? "0" + time : time
                }), ONE_MINUTE < ++time && RManager.stop());
            }, 1e3);
        }), wx.setInnerAudioOption({
            obeyMuteSwitch: !1
        }), audioContext.onStop(onStop), audioContext.onError(onStop), audioContext.onPause(onStop), 
        audioContext.onEnded(onStop), audioContext.onPlay(function() {
            g.setData(_defineProperty({}, chat, "play"));
        });
    },
    onReady: function() {
        pages = iosTop = 1, toid = g.options.toid, fromid = app.userInfo.id || wx.getStorageSync("userInfo").id, 
        request({
            url: "Chat",
            data: {
                type: "getinfo",
                toid: toid,
                fromid: fromid,
                from_role: wx.getStorageSync("page") ? 2 : 1
            },
            showLoading: !1,
            success: function(t) {
                g.setData(_extends({}, t)), getChatList();
            }
        });
    },
    onPageScroll: function(t) {
        0 == t.scrollTop && iosTop ? (iosTop = 0, getChatList(++pages)) : 0 != t.scrollTop || iosTop || (iosTop = 1);
    },
    say: function(t) {
        (0, _tools.subscribe)("USER"), g.data.is_send || send();
    },
    input: function(t) {
        t.detail.value;
        this.setData({
            content: t.detail.value,
            is_send: t.detail.cursor < 1
        });
    },
    send_img: function() {
        (0, _tools.subscribe)("USER"), wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            success: function(t) {
                wx.showLoading({
                    title: "发送中...",
                    mask: !0
                }), (0, _tools.uploadImg)(t.tempFilePaths, function(t) {
                    return send(t, 2);
                });
            }
        });
    },
    voice2text: function() {
        g.setData({
            is_voice: !g.data.is_voice
        });
    },
    touchstart: function(t) {
        audioContext.stop(), app.is_say = !0, is_cancel = !1, g.setData({
            is_touch: !0
        }), (0, _tools.getNodeInfo)({
            id: "#voice_cancel",
            success: function(t) {
                top = t.top, right = t.right, bottom = t.bottom, left = t.left;
            }
        }), RManager.start({
            format: "mp3"
        });
    },
    touchend: function(t) {
        RManager.stop(), g.setData({
            is_touch: !1,
            is_close_chooes: !1
        }), app.is_say = !1;
    },
    touchmove: function(t) {
        var e = t.touches[0], t = e.clientX, e = e.clientY;
        is_cancel = !1, left <= t && t <= right && top <= e && e <= bottom ? (g.setData({
            is_close_chooes: !0
        }), is_cancel = !0) : g.data.is_close_chooes && g.setData({
            is_close_chooes: !1
        });
    },
    imgLoad: function() {
        pages < 2 && (0, _tools.scrollBottom)();
    },
    copy: function(t) {
        wx.setClipboardData({
            data: t.currentTarget.dataset.content
        });
    },
    action: function(t) {
        var e = parseInt(t.currentTarget.dataset.type), o = t.currentTarget.dataset.index, a = g.data.chat[o];
        switch (e) {
          case 2:
            var n = g.data.system.attachurl + a.content;
            wx.previewImage({
                urls: [ n ],
                current: n
            });
            break;

          case 3:
            chat && (g.setData(_defineProperty({}, chat, "")), audioContext.stop()), chat = "chat[" + o + "].class", 
            audioContext.src = g.data.system.attachurl + a.content, audioContext.play();
        }
    },
    onUnload: function() {
        audioContext.stop();
    }
});